package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.constant.RedisMessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.service.MenuService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @RequestMapping("/findMenu")
    public Result findMenu(){
        try {
            List<Menu> menuList = menuService.findMenu();
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    @RequestMapping("/findPower")
    public Result findPower(){
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            List<String> powers = menuService.findPower(user.getUsername());
            return new Result(true, MessageConstant.GET_POWER_SUCCESS,powers);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_POWER_FAIL);
        }
    }


}
